package com.example.tms.controller;

import com.example.tms.bean.*;
import com.example.tms.mapper.AreaMapper;
import com.example.tms.mapper.CstmrMapper;
import com.example.tms.tools.GlobalParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
public class AreaController {

    private final AreaMapper areaMapper;
    private final CstmrMapper cstmrMapper;

    @Autowired
    public AreaController(AreaMapper areaMapper, CstmrMapper cstmrMapper){
        this.areaMapper = areaMapper;
        this.cstmrMapper = cstmrMapper;
    }

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    //查询所有区域信息列表
    @RequestMapping(value = "area",method = RequestMethod.GET)
    public ReturnInfo getAreaInfoAll(){
        return ReturnInfo.info2(areaMapper.findAll());
    }

    //查询区域信息列表(客户id)
    @RequestMapping(value = "area/{khid}",method = RequestMethod.GET)
    public ReturnInfo getAreaInfoKh(@PathVariable String khid){
        return ReturnInfo.info2(areaMapper.findByKhidAndSfczOrderByQynumDesc(khid, true));
    }

    //查询区域信息(区域id)
    @RequestMapping(value = "area/info/{qyid}",method = RequestMethod.GET)
    public AreaInfo getAreaInfo(@PathVariable String qyid){
        return areaMapper.findByQyidAndSfcz(qyid, true);
    }

    //新建区域信息
    @RequestMapping(value = "area",method = RequestMethod.POST)
    public List<Map<String,Object>> saveAreaInfo(@RequestBody AreaInfo areaInfo){
        List<Map<String,Object>>  list = new ArrayList<>();
        Map<String,Object>  map = new HashMap<>();
        if(areaInfo.getKhid()==null||cstmrMapper.findByKhid(areaInfo.getKhid())==null){
            map.put("khid", "0");//客户不存在
        }
        if(!areaInfo.getLxr().equals("")&&!checkLxr(areaInfo.getLxr())){
            map.put("lxr", "0");
        }
        if(!areaInfo.getLxfs().equals("")&&!checkLxfs(areaInfo.getLxfs())){
            map.put("lxfs", "0");
        }
        list.add(map);
        if(map.isEmpty()){
            areaInfo.setQynum(areaMapper.findCountByKhidAndSfcz(areaInfo.getKhid())+1);
            areaInfo.setSfcz(true);
            areaMapper.save(areaInfo);
            logger.info("新建区域 "+areaInfo.toString());
        }
        return list;
    }

    //批量新建充值信息
    @RequestMapping(value = "areas",method = RequestMethod.POST)
    public List<List<Map<String,Object>>> saveAreaInfos(@RequestBody List<AreaInfo> areaInfoList){
        List<List<Map<String,Object>>>  reslist = new ArrayList<>();
        for (AreaInfo areaInfo:areaInfoList) {
            List<Map<String,Object>>  list = new ArrayList<>();
            Map<String,Object>  map = new HashMap<>();
            if(areaInfo.getKhid()==null||cstmrMapper.findByKhid(areaInfo.getKhid())==null){
                map.put("khid", "0");//客户不存在
            }
            if(!areaInfo.getLxr().equals("")&&!checkLxr(areaInfo.getLxr())){
                map.put("lxr", "0");
            }
            if(!areaInfo.getLxfs().equals("")&&!checkLxfs(areaInfo.getLxfs())){
                map.put("lxfs", "0");
            }
            list.add(map);
            reslist.add(list);
            if(map.isEmpty()){
                areaInfo.setQynum(areaMapper.findCountByKhidAndSfcz(areaInfo.getKhid())+1);
                areaInfo.setSfcz(true);
                areaMapper.save(areaInfo);
                logger.info("新建充值信息 "+areaInfo.toString());
            }
        }
        return reslist;
    }

    //更新区域信息
    @RequestMapping(value = "area",method = RequestMethod.PUT)
    public List<Map<String,Object>> updateAreaInfo(@RequestBody AreaInfo areaInfo){
        List<Map<String,Object>>  list = new ArrayList<Map<String,Object>>();
        Map<String,Object>  map = new HashMap<String,Object>();
        if(areaMapper.findByQyidAndSfcz(areaInfo.getQyid(), true)==null){
            map.put("qyid", "0");//该区域不存在
        }
        if(areaInfo.getKhid()==null||cstmrMapper.findByKhid(areaInfo.getKhid())==null){
            map.put("khid", "0");//客户不存在
        }
        if(!areaInfo.getLxr().equals("")&&!checkLxr(areaInfo.getLxr())){
            map.put("lxr", "0");
        }
        if(!areaInfo.getLxfs().equals("")&&!checkLxfs(areaInfo.getLxfs())){
            map.put("lxfs", "0");
        }
        list.add(map);
        if(map.isEmpty()){
            areaInfo.setSfcz(true);
            areaInfo.setQynum(areaMapper.findByQyidAndSfcz(areaInfo.getQyid(), true).getQynum());
            areaMapper.saveAndFlush(areaInfo);
            logger.info("更新区域 "+areaInfo.toString());
        }
        return list;
    }

    //删除区域信息（假删除 区域id）
    @RequestMapping(value = "area/{qyid}",method = RequestMethod.DELETE)
    public ReturnInfo deleteAreaInfo(@PathVariable String qyid){
        String b;
        if(areaMapper.findByQyidAndSfcz(qyid,true)==null){
            b = "该区域不存在！";
        }else{
            AreaInfo areaInfo = areaMapper.findByQyidAndSfcz(qyid,true);
            int qynum = areaInfo.getQynum();
            String khid = areaInfo.getKhid();
            areaMapper.updatePart(khid,qynum);
            areaMapper.DeleteByQyid(qyid);
            b = "删除成功！";
            logger.info("删除区域 "+qyid);
        }
        return ReturnInfo.info1(b);
    }

    //区域上移
    @RequestMapping(value = "area/up/{khid}/{qyid}",method = RequestMethod.PUT)
    public ReturnInfo upAreaInfo(@PathVariable String khid, @PathVariable String qyid){
        String b;
        if(khid==null||cstmrMapper.findByKhid(khid)==null){
            b = "该客户不存在！";
        }else if(qyid==null||areaMapper.findByQyidAndSfcz(qyid, true)==null){
            b = "该区域不存在！";
        }else if(areaMapper.findByQyidAndSfcz(qyid,true).getQynum()<=1){
            b = "该区域不能下移！";
        }else {
            int i = areaMapper.findByQyidAndSfcz(qyid,true).getQynum();
            String qyid2 = areaMapper.findByKhidAndQynum(khid, i-1).getQyid();
            areaMapper.upArea(khid, qyid);     //将指定区域上移
            areaMapper.downArea(khid, qyid2);      //将指定区域上一个区域下移
            b = "区域下移成功！";
        }
        return ReturnInfo.info1(b);
    }

    //区域下移
    @RequestMapping(value = "area/down/{khid}/{qyid}",method = RequestMethod.PUT)
    public ReturnInfo downAreaInfo(@PathVariable String khid, @PathVariable String qyid){
        String b;
        if(khid==null||cstmrMapper.findByKhid(khid)==null){
            b = "该客户不存在！";
        }else if(qyid==null||areaMapper.findByQyidAndSfcz(qyid, true)==null){
            b = "该区域不存在！";
        }else if(areaMapper.findByQyidAndSfcz(qyid,true).getQynum()>=areaMapper.findCountByKhidAndSfcz(khid)){
            b = "该区域不能上移！";
        }else {
            int i = areaMapper.findByQyidAndSfcz(qyid,true).getQynum();
            String qyid2 = areaMapper.findByKhidAndQynum(khid, i+1).getQyid();
            areaMapper.downArea(khid, qyid);       //将指定区域下移
            areaMapper.upArea(khid, qyid2);    //将指定区域下一个区域上移
            b = "区域上移成功！";
        }
        return ReturnInfo.info1(b);
    }

    //检查联系人格式
    private Boolean checkLxr(String  lxr){
        Boolean b = false;
        if(lxr.matches("^[\\u4E00-\\u9FA5A-Za-z]{1,10}$")){
            b = true;
        }
        return b;
    }

    //检查联系方式格式
    private Boolean checkLxfs(String lxfs){
        Boolean b = false;
        if(lxfs.matches("(^1[0-9]{10}$)")||lxfs.matches("(^(\\d{2,4}[-_－—]?)?\\d{3,8}([-_－—]?\\d{3,8})?([-_－—]?\\d{1,7})?$)|(^0?1[35]\\d{9}$)")){
            b = true;
        }
        return b;
    }
}
